﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post.Domain;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using System.Text;
using WuDada.Core.Post.DTO;
using WuDada.Core.Post.DTOConverter;

public partial class index : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;

    //近期活動NodeId=6
    private int m_TopNodeId = 6;
    //成功見證NodeId=13
    private int m_NodeId = 13;
    //首頁小圖NodeId2=21
    private int m_NodeId2 = 21;

    private int m_CurrentActPage
    {
        get { if (ViewState["currentActPage"] == null) { ViewState["currentActPage"] = 0; } return int.Parse(ViewState["currentActPage"].ToString()); }
        set { ViewState["currentActPage"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();

        if (!IsPostBack)
        {
            //講座介紹
            RepeaterAct2_Bind();
            //近期活動活動
            RepeaterAct_Bind();
            //成功見證
            RepeaterSuccess_Bind();
        }
    }

    private void RepeaterAct2_Bind()
    {
        DateTime? startDate = DateTime.Today;
        string sortField = "ShowDate";
        bool sortDesc = true;
        int pageSize = 6;

        IList<PostVO> postList = m_PostService.GetPostListByNodeIdWithCloseDate(m_NodeId2, true, startDate, m_CurrentActPage, pageSize, sortField, sortDesc);
        //假如沒有講座介紹，且pageIndex > 0，則重新從第一頁開始抓
        if ((postList != null && postList.Count == 0) && m_CurrentActPage > 0)
        {
            m_CurrentActPage = 0;
            postList = m_PostService.GetPostListByNodeIdWithCloseDate(m_NodeId2, true, startDate, m_CurrentActPage, pageSize, sortField, sortDesc);
            //RepeaterAct2_Bind();
        }

        IList<ReportPostVO> reportList = PostVOConverter.ToDataTransferObjects(postList, 6);
        rpAct2.DataSource = reportList;
        rpAct2.DataBind();
    }

    protected void lnkAct2Back_Click(object sender, EventArgs e)
    {
        if (m_CurrentActPage > 0)
        {
            m_CurrentActPage -= 1;
            RepeaterAct2_Bind();
        }
    }
    protected void lnkAct2Next_Click(object sender, EventArgs e)
    {
        m_CurrentActPage += 1;
        RepeaterAct2_Bind();
    }

    protected void rpAct2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            ReportPostVO reportPostVO = (ReportPostVO)e.Item.DataItem;

            //標題
            int titleLength = 7;
            if (reportPostVO.Post1 != null)
            {
                string title1 = GetTitle(reportPostVO.Post1.Title, titleLength);
                UIHelper.SetLiteralText(ctrl, "ltlTitle1", title1);
                string img1 = string.Format("<img src='upload/{0}' width='125' height='78' class='act-pp' />"
                    , reportPostVO.Post1.PicFileName);
                UIHelper.SetLiteralText(ctrl, "ltlImg1", img1);
            }
            if (reportPostVO.Post2 != null)
            {
                string title2 = GetTitle(reportPostVO.Post2.Title, titleLength);
                UIHelper.SetLiteralText(ctrl, "ltlTitle2", title2);
                string img2 = string.Format("<img src='upload/{0}' width='125' height='78' class='act-pp' />"
                    , reportPostVO.Post2.PicFileName);
                UIHelper.SetLiteralText(ctrl, "ltlImg2", img2);
            }
            if (reportPostVO.Post3 != null)
            {
                string title3 = GetTitle(reportPostVO.Post3.Title, titleLength);
                UIHelper.SetLiteralText(ctrl, "ltlTitle3", title3);
                string img3 = string.Format("<img src='upload/{0}' width='125' height='78' class='act-pp' />"
                    , reportPostVO.Post3.PicFileName);
                UIHelper.SetLiteralText(ctrl, "ltlImg3", img3);
            }
            if (reportPostVO.Post4 != null)
            {
                string title4 = GetTitle(reportPostVO.Post4.Title, titleLength);
                UIHelper.SetLiteralText(ctrl, "ltlTitle4", title4);
                string img4 = string.Format("<img src='upload/{0}' width='125' height='78' class='act-pp' />"
                    , reportPostVO.Post4.PicFileName);
                UIHelper.SetLiteralText(ctrl, "ltlImg4", img4);
            }
            if (reportPostVO.Post5 != null)
            {
                string title5 = GetTitle(reportPostVO.Post5.Title, titleLength);
                UIHelper.SetLiteralText(ctrl, "ltlTitle5", title5);
                string img5 = string.Format("<img src='upload/{0}' width='125' height='78' class='act-pp' />"
                    , reportPostVO.Post5.PicFileName);
                UIHelper.SetLiteralText(ctrl, "ltlImg5", img5);
            }
            if (reportPostVO.Post6 != null)
            {
                string title6 = GetTitle(reportPostVO.Post6.Title, titleLength);
                UIHelper.SetLiteralText(ctrl, "ltlTitle6", title6);
                string img6 = string.Format("<img src='upload/{0}' width='125' height='78' class='act-pp' />"
                    , reportPostVO.Post6.PicFileName);
                UIHelper.SetLiteralText(ctrl, "ltlImg6", img6);
            }
        }
    }

    private string GetTitle(string title, int titleLength)
    {
        string result = title;
        if (!string.IsNullOrEmpty(result))
        {
            if (result.Length > titleLength)
            {
                result = result.Substring(0, titleLength) + "...";
            }
        }
        return result;
    }

    private void RepeaterSuccess_Bind()
    {
        //搜尋條件
        DateTime? startDate = DateTime.Today;
        string sortField = "ShowDate";
        bool sortDesc = true;

        rpSuccess.DataSource = m_PostService.GetPostListByNodeIdWithCloseDate(m_NodeId, true, startDate, 0, 5, sortField, sortDesc);
        rpSuccess.DataBind();
    }

    protected void rpSuccess_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;

            //標題
            int titleLength = 13;
            string title = postVO.Title;
            if (!string.IsNullOrEmpty(title))
            {
                if (title.Length > titleLength)
                {
                    title = title.Substring(0, titleLength) + "...";
                }
            }
            UIHelper.SetLiteralText(ctrl, "ltlTitle", title);
        }
    }

    private void RepeaterAct_Bind()
    {
        //搜尋條件
        DateTime? startDate = DateTime.Today;

        StringBuilder sbHql = new StringBuilder();
        sbHql.Append(string.Format(@"WHERE Flag = {0} 
AND Node.ParentNode.NodeId = {1} AND CloseDate >= '{2}' "
           , 1, m_TopNodeId, startDate.Value.ToShortDateString()));

        sbHql.Append(" ORDER BY ShowDate Desc ");

        rpAct.DataSource = m_PostService.SearchPostByWhere(sbHql.ToString(), 0, 4);
        rpAct.DataBind();
    }

    protected void rpAct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;

            //日期
            string date = postVO.ShowDate.Value.Year.ToString() + postVO.ShowDate.Value.ToString(".MM.dd");
            UIHelper.SetLiteralText(ctrl, "ltlDate", date);

            //標題
            int titleLength = 24;
            string title = postVO.Title;
            if (!string.IsNullOrEmpty(title))
            {
                if (title.Length > titleLength)
                {
                    title = title.Substring(0, titleLength) + "...";
                }
            }
            UIHelper.SetLiteralText(ctrl, "ltlTitle", title);
        }
    }

}